<template>
  <van-cell class="comment-item">
    <van-image
      slot="icon"
      width="36"
      height="36"
      round
      fit="cover"
      :src="comment.aut_photo"
    ></van-image>
    <div slot="title" class="title-wrap">
      <div class="user-name">{{ comment.aut_name }}</div>
      <van-button
        @click="onCommentLike"
        :loading="commentLoading"
        class="like-btn"
        :class="{ like: comment.is_liking }"
        :icon="comment.is_liking ? 'good-job' : 'good-job-o'"
      >
        {{ comment.like_count || "赞" }}
      </van-button>
    </div>
    <div slot="label">
      <p>{{ comment.content }}</p>
      <div class="botton-info">
        <span class="comment-pubdate">{{ comment.pubdate }}</span>
        <van-button class="reply-btn" round size="mini">
          回复{{ comment.reply_count }}
        </van-button>
      </div>
    </div>
  </van-cell>
</template>

<script>
import { addCommonlike, deleteCommonlike } from "@/api/comment";
export default {
  name: "CommentItem",
  props: {
    comment: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      commentLoading: false,
    };
  },
  methods: {
    async onCommentLike() {
      this.commentLoading = true;
      try {
        if (this.comment.is_liking) {
          // 已点赞，取消点赞
          await deleteCommonlike(this.comment.com_id);
          this.comment.like_count--;
          this.comment.is_liking = false;
        } else {
          // 没点赞，添加点赞
          await addCommonlike(this.comment.com_id);
          this.comment.like_count++;
          this.comment.is_liking = true;
        }
      } catch (err) {}
      this.commentLoading = false;
    },
  },
};
</script>

<style scoped lang="less">
.comment-item {
  .van-image {
    margin-right: 10px;
  }
  /deep/ .van-cell__title {
    .title-wrap {
      display: flex;
      justify-content: space-between;
    }
    .van-button {
      height: 30px;
    }

    .reply-btn {
      margin-left: 20px;
    }
    .like {
      color: red;
    }
  }
}
</style>
